package jetpack.com.pzl.architecture.data_binding;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableArrayMap;
import androidx.databinding.ObservableList;
import androidx.databinding.ObservableMap;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import jetpack.com.pzl.R;
import jetpack.com.pzl.databinding.ActivityDataBindingBinding;

public class DataBindingActivity extends AppCompatActivity {
    String TAG = "DataBindingActivity";

    Random random = new Random();

    ActivityDataBindingBinding binding;

    Bean1 bean1 = new Bean1("以歌之名", "细节不息", 1.99f);
    Bean2 bean2 = new Bean2("克格莫", 33, "璐璐、甘娜");

    private ObservableList<String> list = new ObservableArrayList<>();
    private ObservableMap<String, String> map = new ObservableArrayMap<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //系统会为每个布局文件生成一个绑定类。默认情况下，类名称基于布局文件的名称，它会转换为 Pascal 大小写形式并在末尾添加 Binding 后缀。
        binding = DataBindingUtil.setContentView(this, R.layout.activity_data_binding);
        UserBean user = new UserBean("姜", "汉娜");
        //此类包含从布局属性（例如，user 变量）到布局视图的所有绑定
        binding.setUser(user);

        list.add("自缚剑");
        list.add("兼容弟");
        list.add("腻歪棚");

        map.put("0", "商店寺");
        map.put("1", "企鹅册");
        map.put("2", "到呢神");

        BeanTextView beanTextView = new BeanTextView();
        binding.setBeanTextView(beanTextView);
    }

    @Override
    protected void onResume() {
        super.onResume();

        binding.setCommon(new CommonBean("commonBeam : 向死而生"));
        binding.setCommon2(new jetpack.com.pzl.architecture.data_binding.same.CommonBean("commonBeam2 : 怀疑是最强大的敌人"));

        binding.setBean1(bean1);
        binding.setBean2(bean2);
        binding.setDataSourceBeanChanger(new DataSourceBeanChanger());

        binding.setMap(map);
        binding.setList(list);

        binding.setEventCallBack(new EventCallBackClz());

        binding.setCtx(this);
    }

    public void click(View v) {
        switch (v.getId()) {
            case R.id.btnArchitectureDataBindingSet1:
                UserBean user = new UserBean("song", "jihyo");
                binding.setUser(user);
                break;
            case R.id.btnArchitectureDataBindingListView:
                if (binding.getAdapter1() == null) {
                    List<ListItemBean> data = new ArrayList<ListItemBean>();
                    Random r = new Random();
                    int n = r.nextInt(10);
                    for (int i = 0; i <= n; i++) {
                        data.add(new ListItemBean("ListItemBean" + i));
                    }
                    binding.setAdapter1(new ListAdapter(data, this));
                } else {
                    binding.getAdapter1().getData().clear();
                    Random r = new Random();
                    int n = r.nextInt(10);
                    for (int i = 0; i <= n; i++) {
                        binding.getAdapter1().getData().add(new ListItemBean("ListItemBean" + i));
                    }
                    binding.getAdapter1().notifyDataSetChanged();
                }
                break;
            case R.id.btnDataBindingBaseObservable:
                //改变源数据，数据驱动UI
                break;
            case R.id.btnDataBindingObservableList:
                int index = random.nextInt(3);
                binding.setIndex(index);
                break;
            case R.id.btnDataBindingObservableMap:
                String key = Integer.toString(random.nextInt(3));
                binding.setKey(key);
                break;
            case R.id.btnDataBindingAdapterShow:
                Random r = new Random();
                int txtsize = r.nextInt(16);
                int padding = r.nextInt(100);
                byte[] txt = new byte[20];
                r.nextBytes(txt);
                String text = new String(txt);
                String R = Integer.toString(r.nextInt(9) + 1);
                String G = Integer.toString(r.nextInt(9) + 1);
                String B = Integer.toString(r.nextInt(9) + 1);
                String txtColor = "#" + R + R + G + G + B + B;
                R = Integer.toString(r.nextInt(9) + 1);
                G = Integer.toString(r.nextInt(9) + 1);
                B = Integer.toString(r.nextInt(9) + 1);
                String bgColor = "#" + R + R + G + G + B + B;
                BeanTextView beanTextView = new BeanTextView();
                beanTextView.bgColor = bgColor;
                beanTextView.txtColor = txtColor;
                beanTextView.padding = padding;
                beanTextView.text = text;
                beanTextView.txtSize = txtsize;
                binding.setBeanTextView(beanTextView);
                break;
        }
    }


    //数据改变来驱动UI更新
    public class DataSourceBeanChanger {
        Thread r1, r2;
        boolean start1, start2;

        public DataSourceBeanChanger() {
            final Random random = new Random();
            r1 = new Thread() {
                @Override
                public void run() {
                    while (start1) {
                        start1 = true;
                        bean1.name = "以" + Long.toHexString(random.nextLong()) + "之名";
                        bean1.setDetails("细节 " + Long.toHexString(random.nextLong()));
                        //没  @Bindable注解，所以不更新ui
                        bean1.setPrice(random.nextFloat());
                        try {
                            Thread.sleep(200);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            };

            r2 = new Thread() {
                @Override
                public void run() {
                    while (start2) {
                        start2 = true;
                        bean2.getHero().set(Long.toHexString(random.nextLong()) + "克格莫");
                        bean2.getAttack().set(random.nextInt());
                        bean2.getPartner().set(Long.toHexString(random.nextLong()) + "璐璐，甘娜");
                        try {
                            Thread.sleep(200);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            };

        }

        public void change(int i) {
            Log.i(TAG, "DataSourceBean1Changer change");
            if (i == 1 && start1 == false) {
                Log.i(TAG, "DataSourceBean1Changer change r1.start1");
                start1 = true;
                r1.start();
            }
            if (i == 2 && start2 == false) {
                Log.i(TAG, "DataSourceBean1Changer change r2.start2");
                start2 = true;
                r2.start();
            }
        }
    }


    public class EventCallBackClz {
        public void onGetBean(Bean2 bean2) {
            Toast.makeText(DataBindingActivity.this, bean2.getHero().get() + "\n" +
                    bean2.getAttack().get() + "\n" + bean2.getPartner().get(), Toast.LENGTH_SHORT).show();
        }
    }


}
